
/**
** \file       list.c
**
** \brief      this function parse the list rule
**
** \author     Ben M'rad Hanna, Laterem Hamza, Boillot Ulysse
**             and De Olivera Guillaume
**
** \version    0.8
**
** \date       26 Novembre 2010
**
*/

#include "rules.h"

static struct andor     *aux(struct andor        *tmp_andor)
{
  free(tmp_andor);
  tmp_andor = NULL;

  if (!strcmp(g_data->lexer->token->id, ";") ||
      !strcmp(g_data->lexer->token->id, "&"))
    g_data->lexer = g_data->lexer->next;

  return (tmp_andor);
}

static s_list           *aux2(struct andor       *andor,
                              s_list             *list)
{
  free(andor);
  andor = NULL;

  return (list);
}

s_list                  *rule_list(s_list       *list)
{
  s_andor       *tmp_andor = NULL;
  s_andor       *last = NULL;

  list->andor = calloc(1, sizeof (struct andor));
  list->andor->next = NULL;

  if (g_data->lexer == NULL)
    return (NULL);

  if (rule_and_or(list->andor) != NULL)
    {
      last = list->andor;
      tmp_andor = list->andor->next;

      while (strcmp(g_data->lexer->token->id, ";") == 0
	     || strcmp(g_data->lexer->token->id, "&") == 0)
	{
	  g_data->lexer = g_data->lexer->next;

	  if (rule_and_or(tmp_andor) == NULL)
	    return (aux2(tmp_andor, list));

	  last = tmp_andor;
	  tmp_andor = tmp_andor->next;
	}

      last->next = aux(last->next);

      return (list);
    }

  return (aux2(list->andor, NULL));
}
